var chargingRecordHandleData = [];
var totalPage = 0;
var totalCnt = 0;

//查询
$(document).ready(function() {
	showLoading();
	$('title', parent.document).html($('title').text());
	setBaseUrl();
	createTableHead("chargingRecordHandleThead", chargingRecordHandleTable, search);
	scrollTableSuspend("scrollBody", "chargingRecordHandleTable", "chargingRecordHandleTable1", "chargingRecordHandleThead", "scrollHeight");
	$ajaxList('chargingStation', getChargingStation, {BulidStatus: '3'});
	search();
});
function getChargingStation(data) {
	var length = data.length;
	for (var i = 0; i < length; i++) {
		$("#chargingStationID").append("<option value='" + data[i].chargingStationID + "'>" + data[i].chargingStationName + "</option>");
	}
}
function changeEquipmet(){
	var chargingStationID = $("#chargingStationID").val();
	if (chargingStationID) {
		$("#changeEquipment").show();
		$("#equipmentID").empty();
		$ajaxList('equipmentNo', getEquipmentNo, {"chargingStationID": chargingStationID});
	} else {
		$("#changeEquipment").hide();
		$("#equipmentID").empty();
	}
}
function getEquipmentNo(data) {
	var length = data.length;
	$("#equipmentID").append("<option value=''>请选择</option>");
	for (var i = 0; i < length; i++) {
		$("#equipmentID").append("<option value='" + data[i].equipmentID + "'>" + data[i].equipmentName + "</option>");
	}
}
function showTip(i){
	$("#" + chargingRecordHandleTable[i].headerNameEN).tips({
		side : 1,
		msg : chargingRecordHandleTable[i].message,
		bg : '#AE81FF',
		time : 1.5
	});
}
function search(){
	$ajax('searchChargingRecordHandle', 'chargingRecordHandleSearch', searchChargingRecordHandleData);
}
function searchChargingRecordHandleData(data) {
	totalPage = data && data.totalPage || 0;
	totalCnt = data && data.totalCnt || 0;
	chargingRecordHandleData = data && data.chargingRecordHandleList || [];
	createTableBody("chargingRecordHandleTbody", chargingRecordHandleTable, chargingRecordHandleData);
	setTimeout(hideLoading, 500);
}
function seeMessage(index){
	if (chargingRecordHandleData[index].message != null){
		$("#message").val(chargingRecordHandleData[index].message.replace(/,/g, "\r\n"));
	} else {
		$("#message").val('充电报文不存在');
	}
	$("#Message").modal("show");
}
function messageSure(){
	$("#Message").modal("hide");
}
//修改
function update() {
	var checkbox = getCheckbox("chk");
	var count = checkbox && checkbox.count;
	var index = checkbox && checkbox.row_index;
    if (count == 1) {
    	$('#title').html('');
    	$("#ID").val(chargingRecordHandleData[index].id);
    	deleteFlag = "update";
    	addTitle("chargingRecordDetails");
    	allTable(index);
    } else if (count == 0) {
    	showMessge(messageContent["Select.NotFound"]);
    } else {
    	showMessge(messageContent["Select.OnlyOne"]);
    }
}
function allTable(index){
	createDetailHead("detailThead", chargingRecordHandleTable, 1);
	createDetailBody("detailTbody", chargingRecordHandleTable, chargingRecordHandleData[index], 1);
	createDetailAll("detailTable", chargingRecordHandleTable, chargingRecordHandleData[index], 2);
	if(deleteFlag == "details"){
		setFormDisabled("saveForm");
		$('#confirm').hide();
	} else {
		$('#confirm').show();
	}
	$("#save").modal("show");
}
function confirm(){
	return checkForm("saveForm", chargingRecordHandleTable);
}
function getUrl(){
	if(deleteFlag == "update"){
		return 'updateChargingRecordHandle';
	} else if(deleteFlag == "delete"){
		return 'deleteChargingRecordHandle';
	}
}
function getData(){
	return {chargingRecordList: getCheckDataList(chargingRecordHandleData, "id")};
}
function saveAbnormalChargingRecord(data){
     var result = data && data.result;
     if (result == "Success"){
    	 showMessge(messageContent["Update.Success"]);
    	 $('#save').modal('hide');
    	 formReset("saveForm");
     } else if (result == "NotFound"){
    	 showMessge(messageContent["Update.Failed"]);
     } else{
    	 showMessge(messageContent["Update.Error"]);
     }
     search();
}
function sure(data){
	if(deleteFlag == "update"){
		showMessge("导出中......");
		$ajax('exportChargingRecordHandle?checkFlag=', undefined, exportFileResult);
	} else {
		var result = data && data.result;
		if (result == "Success") {
			showMessge(messageContent["Delete.Success"]);
		} else if (result == "NotFound") {
			showMessge(messageContent["Delete.NotFound"]);
		} else {
			showMessge(messageContent["Delete.Error"]);
		}
		search();
	}
}
function exportFile(){
	if (chargingRecordHandleData.length > 0) {
		showMessge("确定导出吗?", "model2");
		deleteFlag == "export"
	} else {
		showMessge("没有数据可导出！");
	}
}
function exportFileResult(data){
	var result = data && data.result;
	if (result == "Success") {
		showLoading();
		downloadFile('exportChargingRecordHandle?checkFlag=OK');
		showMessge("导出成功，充电记录文件正在生成！");
		setTimeout(hideLoading, 2000);
	} else if (result == "JsonError") {
		showMessge("导出失败，导出的JSON模板错误 或 不存在！");
	} else if (result == "NotFound") {
		showMessge("导出失败，没有数据可导出！");
	} else {
		showMessge("导出失败，请重新导出 或 联系管理员！");
	}
}